package com.imoblife.brainwavestus.customview;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.imoblife.brainwavestus.BrainWavestusApplication;
import com.imoblife.brainwavestus.ProductDetailActivity;
import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.customview.AbsProductList.OnItemListener;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;
import com.imoblife.brainwavestus.util.DebugServerPushDataUtil;
import com.imoblife.brainwavestus.util.DebugUtil;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class FreatureProductsView extends BasicDisplay {
	final String TAG = "FreatureProductsView";
	AbsProductList mAbsProductList;
	
	private Store mStore;
	
	public FreatureProductsView(Activity activity) {
		super(activity);
		BrainWavestusApplication app
				= (BrainWavestusApplication)activity.getApplication();
		mStore = app.getStore();
	}

	public void loading() {
		mProductsDate = new ArrayList<String>();
		mAbsProductList = (AbsProductList) mActivity
				.findViewById(R.id.product_list);
		mAbsProductList.setVisibility(View.VISIBLE);
		initalizeProductsNode();
		initializeProductsView();
	}

	protected void initalizeProductsNode() {
		mAbsProductList.setProductLayout(R.layout.tb_featured_product_node);
	}

	protected void initializeProductsView() {

		mAbsProductList.setDataList(mStore.getHotSaleProductNames());

		mAbsProductList.setOnItemClicker(new OnItemListener() {

			public void onItemClick(View view, String productId) {

				Intent intent = new Intent(mActivity, ProductDetailActivity.class);
				intent.putExtra(Constant.PRODUCT_ID, productId);
				mActivity.startActivity(intent);
			}

			public void onItemLoading(View parent, String productId) {

				DebugUtil.debug(TAG, "feature icon>>" + parent.getHeight() + ">>"
						+ parent.getWidth());

				ImageView productIcon = (ImageView) parent
						.findViewById(R.id.product_icon);

				if (isXLargerScreen()) {
					LayoutParams params = productIcon.getLayoutParams();
					ImageView productIconPrev = (ImageView) parent
							.findViewById(R.id.product_icon_prev);

					if (parent.getLayoutParams() != null) {
						params.width = parent.getLayoutParams().height
								- parent.getPaddingTop()
								- parent.getPaddingBottom();
						DebugUtil.debug(TAG, "w=" + params.width);
						DebugUtil.debug(TAG, "h=" + params.height);
						params.width *= 0.8;
						params.height = params.width;
						productIcon.setLayoutParams(params);
						productIconPrev.setLayoutParams(params);
					}
				}

				productIcon.setImageBitmap(BitMapFlyweightFactory
						.getBitMapFlyweightFactory(mActivity.getAssets())
						.getBitMapFlyweight(productId).getBitmap());

				TextView productName = (TextView) parent
						.findViewById(R.id.product_name);
				
				ProductInfo info = mStore.getProductByName(productId).getProductInfo();
				
				String productTitle = info.getTitle();		
				productName.setText(productTitle);			
			}
		});

	}

	List<String> mProductsDate;

	protected List<String> getProductsData() throws IOException, JSONException {
		InputStream inputStream = null;
		if (DebugServerPushDataUtil.isOpenProductJsonDebug) {
			try {
				inputStream = mActivity.openFileInput("products.json");
			} catch (Exception e) {
			}
		}
		if (inputStream == null) {
			inputStream = mActivity.getAssets().open("products.json");
		} else {
			DebugUtil.debug(TAG, "��ȡ�����̵��ļ��ɹ�");
		}

		byte[] data = new byte[inputStream.available()];

		inputStream.read(data);
		inputStream.close();
		String string = new String(data);

		DebugUtil.debug(TAG, string);

		JSONObject jsonObject = new JSONObject(string);

		JSONArray jsonArray = jsonObject.getJSONArray("Featured");

		String[] products = new String[jsonArray.length()];

		for (int i = 0; i < 8; i++) {

			mProductsDate
					.add(jsonArray.getJSONObject(i).getString("productId"));

			DebugUtil.debug(TAG, products[i]);
		}
		return mProductsDate;
	}

	public void release() {
		mAbsProductList.release();
	}

}
